const BASE_URL = 'https://api.scm.yiniao.cc'; //测试服域名
// const BASE_URL  = 'https://api.scm.yiniao.co'; //正式服域名

export const request = (options) => {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token')
		uni.showLoading({
			mask: true,
			title: '加载中...'
		})
		uni.request({
			url: BASE_URL + options.url,
			method: options.method,
			header: {
				token: token || '',
			},
			data: options.data || options.params,
			success: res => {
				uni.hideLoading()
				const data = res.data
				if (data.code !== 0) {

					// token出错 重新登录
					if (data.code == 40009 || data.code == 40007 || data.code == 40006) {
						setTimeout(() => {
							uni.clearStorageSync()
							uni.showToast({
								icon: 'none',
								title: '登陆超时，请重新登录',
								mask: true
							})
						})
						return uni.reLaunch({
							url: '/pages/index/index'
						})
					} else {
						console.log('res.data', res.data);
						return setTimeout(() => {
							uni.showToast({
								icon: 'none',
								title: data.msg || '服务器错误',
								mask: true
							})
						})
					}
				}

				resolve(data)
			},
			fail: error => {
				uni.hideLoading()
				console.log('出现错误', error);
				setTimeout(() => {
					uni.showToast({
						icon: 'none',
						title: error.data.msg || '服务器错误',
						mask: true
					})
				})
				reject(error)
			}
		})
	})
}